﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="http://shiro.apache.org/tags" %>
<%@ include file="/WEB-INF/view/include/inc.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
<%@ include file="/WEB-INF/view/include/jQgrid.jsp"%>
</head>
<body>
	<div class="">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox " style="margin-bottom: 0px;">
					<div class="ibox-title" style="border-color: white;">
							<div class="col-sm-3" >
                                <div class="input-group" >
                                   <span class="input-group-btn">
                                        <a id="find_btn" type="button" class="btn btn-sm btn-primary" onclick="depadd(0);"> 添加</a> </span>
                                </div>
                            </div>
                        <div class="ibox-content">
							<div class="jqGrid_wrapper"  >
								<table id="table_list" ></table>
								<div id="pager_list"></div>
							</div>
						</div>
					</div>
					<!-- tableover -->
				</div>
				<!-- PAGE CONTENT ENDS HERE -->
				
			</div>
			<!--/row-->
		</div>
		<!--/#page-content-->
	</div>
	<!--/.fluid-container#main-container-->
	<script type="text/javascript">
		$(function() {
			$.jgrid.defaults.styleUI = "Bootstrap";
			$("#table_list").jqGrid({
				url: '<%=contextPath %>/dep/getdepBypid',
				ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
				datatype : "json",
				autowidth : true,
				shrinkToFit : true,
				mtype : "get",
				rowNum : 20,
				colNames : ["id", "部门名称","区域地址","地址","创建时间","操作"],
				colModel : [{
					name : "id",
					index : "id",
					hidden:true
				},  {
					name : "depName",
					index : "depName",
					width : 50
				},  {
					name : "title",
					index : "title",
					width : 50
					
				},  {
					name : "address",
					index : "address",
					width : 50
				},  {
					name : "createtime",
					index : "createtime",
					width : 50
				},{
					formatter:function(cellvalue, options, row){
						        var level = Number(row.level)+Number(1);
								return " <s:hasPermission name="dep:admin"><a class='btn btn-mini btn-warning' title='添加子级' onclick='adddep("+row.id+","+level+")'><i class='icon-plus'></i></a></s:hasPermission> "
						     	   +" <s:hasPermission name="dep:admin"><a class='btn btn-mini btn-info' title='修改' onclick='showeditdep("+row.id+")'><i class='icon-edit'></i></a></s:hasPermission> "
								   +" <s:hasPermission name="dep:admin"><a class='btn btn-mini btn-danger' title='删除' onclick='deldep("+row.id+")'><i class='icon-trash'></i></a></s:hasPermission> ";
					},
					width : 100
				} ],
				 treeReader : {  
				      level_field: "level",  
				      parent_id_field: "parent",   
				      leaf_field: "leaf",  
				      expanded_field: "expanded"  
				    },  
			    jsonReader: {      
			        root: "rows",    
			        repeatitems : false      
			      },   
				ExpandColumn: "depName",
				treeGrid: true,
				height : $(window).height() - 130,
				treeGridModel: "adjacency"
			});
			
			$("#table_list").navGrid('#pager_list',{edit:false,add:false,del:false,search:true,refresh:true});
			
			$(window).bind("resize", function() {
				var b = $(".jqGrid_wrapper").width();
				$("#table_list").setGridWidth(b);
				$("#table_list").setGridHeight($(window).height() - 170);
			})
			 
		});
		
		//添加公司
		function depadd(level){
			 var diag = new top.Dialog();
			 diag.Drag=true;
			 diag.Title ="添加公司";
			 diag.URL = "<%=contextPath %>/dep/addView?level="+level;
			 diag.Width = 550;
			 diag.Height = 500;
			 diag.CancelEvent = function(){ //关闭事件
				$("#table_list").trigger("reloadGrid");
				diag.close();
			 };
			 diag.show();
		}
		
		//删除部门
		function deldep(id){
			var url ="<%=contextPath %>/dep/delete";
			swal({
			    title: "您确定要删除该条记录吗",
			    text: "删除后将无法恢复，请谨慎操作！",
			    type: "warning",
			    showCancelButton: true,
			    confirmButtonColor: "#DD6B55",
			    confirmButtonText: "删除",
			    animation:"slide-from-bottom",
			    cancelButtonText:"取消",
			    closeOnConfirm: true
			},function (){
				 $.ajax({url:url+"?id="+id,
					 dataType:'json',
					 success:function(data){
						if(data.code==200){
							toastr.success("删除成功");
							$("#table_list").trigger("reloadGrid");
				    	}else{
				    		toastr.error("出现错误，请稍后再试！");
				    	}
					 },
					error:function(){
						toastr.error("未知错误，请稍后再试！");
					}
				 });
			});
		};
		
		//添加部门
		function adddep(id,level){
			 var diag = new top.Dialog();
			 diag.Drag=true;
			 diag.Title ="添加部门";
			 diag.URL = "<%=contextPath %>/dep/adddepView?id="+id+"&level="+level;
			 diag.Width = 400;
			 diag.Height = 300;
			 diag.CancelEvent = function(){ //关闭事件
				$("#table_list").trigger("reloadGrid");
				diag.close();
			 };
			 diag.show();
		}
		
		//修改部门
		function showeditdep(id){
			 var diag = new top.Dialog();
			 diag.Drag=true;
			 diag.Title ="修改部门";
			 diag.URL = "<%=contextPath %>/dep/showeditdep?id="+id;
			 diag.Width = 550;
			 diag.Height = 500;
			 diag.CancelEvent = function(){ //关闭事件
				$("#table_list").trigger("reloadGrid");
				diag.close();
			 };
			 diag.show();
		}
	
  </script>
</body>
</html>

